package mop.gui.task;

import mop.controller.UpdateTaskStatusController;
import mop.gui.GUI;
import mop.model.task.TaskInterface;

/**
 * TaskStatusDialog
 * 
 * @author  Laurens
 */
public class TaskStatusDialog extends javax.swing.JDialog 
{
    /**
     * TaskStatusDialog
     * 
     * @param   gui
     * @param   updateTaskStatusController
     * @param   task
     */
    public TaskStatusDialog(GUI gui, UpdateTaskStatusController updateTaskStatusController, TaskInterface task)
    {
        super(gui.getMainWindow(), true);
        initComponents();
        
        setGui(gui);
        this.updateTaskStatusController = updateTaskStatusController;

        // Set task
        this.task = task;
        this.txtTask.setText(task.getId());

        // Set current status
        this.cmbStatus.setSelectedItem(task.getStatus().getName());
    }
    
    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/
    
    private GUI gui;
    
    /**
     * Sets the GUI.
     * 
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }
    
    /**
     * Returns the GUI reference.
     * 
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }
    
    /***************************************************************************
     *                          Controllers
     ***************************************************************************/
    
    private UpdateTaskStatusController updateTaskStatusController;
    
    /***************************************************************************
     *                          Selected task
     ***************************************************************************/
    
    private TaskInterface task;
    
    /***************************************************************************
     *                          Dialog-specific functionality
     ***************************************************************************/

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTaskStatus = new javax.swing.JLabel();
        lblTask = new javax.swing.JLabel();
        btnUpdate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtTask = new javax.swing.JTextArea();
        lblNewStatus = new javax.swing.JLabel();
        cmbStatus = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Update Task Status");

        lblTaskStatus.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblTaskStatus.setText("Update Task Status");
        lblTaskStatus.setName("lblTaskStatus"); // NOI18N

        lblTask.setText("Task");
        lblTask.setName("lblTask"); // NOI18N

        btnUpdate.setText("Update");
        btnUpdate.setName("btnUpdate"); // NOI18N
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtTask.setColumns(20);
        txtTask.setEditable(false);
        txtTask.setRows(5);
        txtTask.setName("txtTask"); // NOI18N
        jScrollPane1.setViewportView(txtTask);

        lblNewStatus.setText("Choose new status");
        lblNewStatus.setName("lblNewStatus"); // NOI18N

        cmbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Unfinished", "Successful", "Failed" }));
        cmbStatus.setName("cmbStatus"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                    .addComponent(lblTaskStatus)
                    .addComponent(lblTask)
                    .addComponent(lblNewStatus)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmbStatus, 0, 327, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTaskStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTask)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNewStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

/**
 * Closes this dialog.
 * 
 * @param   evt
 */
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

/**
 * Updates the task status.
 * 
 * @param   evt
 */
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
    try
    {
        updateTaskStatusController.updateTaskStatus(task.getId(), (String)this.cmbStatus.getSelectedItem());
        gui.getMainWindow().update();
        gui.handleSuccess("Status updated!");
        this.dispose();
    } 
    catch (Throwable exception)
    {
        gui.handleError(exception);
    }
}//GEN-LAST:event_btnUpdateActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cmbStatus;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNewStatus;
    private javax.swing.JLabel lblTask;
    private javax.swing.JLabel lblTaskStatus;
    private javax.swing.JTextArea txtTask;
    // End of variables declaration//GEN-END:variables
}